package com.zch.mobile;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.mobile.MobException;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.bf.common.proto.MCommon.MRet;
import com.bf.common.proto.MMineInfo.MineCompanyMsgdetail;
import com.item.dao.model.User;
import com.item.service.UserService;
import com.zch.service.CompanyMessageService;

@Mobile
@Component
public class MAppMine {
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyMessageService companyMessageService;

    /**
     * 我的--我的信息--修改头像昵称
     * @param mobileInfo
     * @param headImage
     * @param nickName
     * @return
     * @throws Exception
     */
    @MobileMethod(methodno = "MMineMsg", isLogin=true)
    public MRet.Builder MineMsg(MobileInfo mobileInfo, String headImage, String nickName)throws Exception{
        
        User user = new User();
        user.setId(mobileInfo.getUserid());
        boolean update = false;
        if (StringUtils.isNotBlank(headImage)){
        	user.setHeadImg(headImage);
        	update = true;
        }
        if (StringUtils.isNotBlank(nickName)){
        	user.setNickName(nickName);
        	user.setIsInitNickName(1);
        	update = true;
        }
        if (update){
            int result = userService.updateByPrimaryKeySelective(user);
            if(result == 0){
                throw new MobException(MZchError.SERVER_BUSY_ERROR);
            }                     
        }
        MRet.Builder ret= MRet.newBuilder();
        ret.setCode(1);
        return ret;
        
    }
    
    /**
     * 我的--我的信息--企业信息
     * @param mobileInfo
     * @param companyName
     * @param address
     * @return
     * @throws Exception
     */
    @MobileMethod(methodno = "MMineCompanyMsg", isLogin=true)
    public MineCompanyMsgdetail.Builder MineCompanyMsg(MobileInfo mobileInfo, String companyName, String address)throws Exception{
        User user = userService.selectByPrimaryKey(mobileInfo.getUserid());
        MineCompanyMsgdetail.Builder c = MineCompanyMsgdetail.newBuilder();
        /*if(companyName != null && address != null){
           user.setFirmName(companyName);
           user.setFirmAddress(address);
           if(StringUtils.isNotBlank(user.getId())){
               int result = userService.insert(user);
               if(result == 0){
                   throw new MobException(MZchError.SERVER_BUSY_ERROR);
               }
           }
           MRet.Builder ret= MRet.newBuilder();
           ret.setCode(1);
           return ret;
        }else{
            MineCompanyMsgdetail.Builder c = MineCompanyMsgdetail.newBuilder();
            c = MineCompanyMsgDetai(user);
            MRet.Builder ret= MRet.newBuilder();
            if(c.getCompanyName()=="" || c.getCompanyName()==null || c.getAddress()=="" || c.getAddress()==null){
                ret.setMsg("企业信息不能空！");
            }
            return ret;
        }*/
        
        //没有传公司信息和注册地址
        if(user.getFirmName()!="" || user.getFirmName()!=null || user.getFirmAddress()!="" || user.getFirmAddress()!=null){
            if(companyName == null && address == null){
                c.setCompanyName(user.getFirmName());
                c.setAddress(user.getFirmAddress());
            }else{
                user.setFirmName(companyName);
                user.setFirmAddress(address);
                int result = userService.updateByPrimaryKeySelective(user);                
                if(result == 0){
                    throw new MobException(MZchError.SERVER_BUSY_ERROR);
                }else if(result == 1){
                    user.setType(2);
                    int data = userService.updateByPrimaryKeySelective(user);
                }
            }
            
        }
        return c;
    }
    
    public MineCompanyMsgdetail.Builder MineCompanyMsgDetai(User user)throws Exception{
        String user_id = user.getId();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", user_id);
        User ex = userService.selectAppList(map);
        if(ex == null){
            throw new MobException(MZchError.CONTENT_NOT_EXIST);
        }
        MineCompanyMsgdetail.Builder c = MineCompanyMsgdetail.newBuilder();
        c.setId(ex.getId());   
        c.setCompanyName(ex.getFirmName());
        c.setAddress(ex.getFirmAddress());
        return c;
    }
}
